<template>
  <div class="login-container">
    <div class="login-left">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3>欢迎使用</h3>
          <h1>余杭蚁事通后台管理系统</h1>
        </div>

        <el-form-item prop="username" label="账号：">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password" label="密码：">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >立即登录</el-button>
        <div class="cipher-handle">
          <div class="Remember-password">
            <el-checkbox v-model="checked">记住密码</el-checkbox>
          </div>
          <div class="Forget-password">忘记密码？</div>
        </div>
      </el-form>
    </div>
    <div class="login-right">
      <div class="bnners">
        <div class="block">
          <el-carousel
            trigger="click"
            arrow="never"
          ><el-carousel-item v-for="item in rotation" :key="item">
            <img :src="item.img" alt="" class="item-img">
            <h3 class="item-title">{{ item.title }}</h3></el-carousel-item>
          </el-carousel>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername, BNNER_LIST } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      rotation: [
        {
          id: 1,
          img: require('@/assets/404_images/mayilogo.png'),
          title: '余杭蚁事通,一键灭蚁，服务零距离。'
        },
        {
          id: 2,
          img: require('@/assets/404_images/mayilogo1.jpg'),
          title: '余杭蚁事通,一键灭蚁，服务零距离。'
        },
        {
          id: 3,
          img: require('@/assets/404_images/mayilogo2.jpg'),
          title: '余杭蚁事通,一键灭蚁，服务零距离。'
        }
      ],
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      checked: false
    }
  },
  computed: {
    bnnerList() {
      return BNNER_LIST()
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
.login-container {
  height: 100%;
  box-sizing: border-box;
  padding: 100px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-wrap: wrap;

  .login-left {
    width: 30%;
    .el-input {
      input {
        width: 80%;
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: #333;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    ::v-deep .el-form-item__content {
      line-height: 40px;
      position: relative;
      font-size: 34px;
      flex: 1;
    }
    .el-form-item {
      background: #fff;
      border-radius: 5px;
      color: #454545;
      display: flex;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: #889aa4;
      cursor: pointer;
      user-select: none;
    }
    .el-button {
      border-radius: 20px;
      background-color: #3a62d7;
    }

    .title-container {
      font-size: 20px;
      color: #3a62d7;
    }
    .cipher-handle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .Forget-password {
        color: #3a62d7;
      }
    }
  }
  .login-right {
    // margin-left: 50px;
    border-radius: 0 20px 20px 0;
    overflow: hidden;
    width: 40%;
    height: 100%;
    .bnners {
      height: 100%;
      position: relative;
      .block {
        height: 100%;
        .item-title {
          position: absolute;
          left: 0;
          bottom: 100px;
          width: 100%;
          text-align: center;
          color: #fff;
          font-size: 18px;
          margin: 0;
        }
        .item-img {
          width: 100%;
          height: 65%;
          background-color: #fff;
        }
        .el-carousel {
          height: 100%;
          ::v-deep .el-carousel__container {
            height: 100%;
          }
        }

        .el-carousel__item {
          background-color: #3a62d7;
        }
        ::v-deep .el-carousel__button {
          width: 10px;
          height: 10px;
          border-radius: 50%;
        }
        .el-carousel__arrow:hover {
          display: none;
        }
      }
    }
  }
}
</style>
